package model;

import controller.LogicBarang;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ModelBarang {

    Connection conn;

    public ModelBarang(Connection conn) {
        this.conn = conn;
    }

    public List<Barang> getAllBarang() throws Exception {
        List<Barang> barang = new ArrayList<Barang>();
        String sql = "SELECT * FROM barang";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Barang brg = new Barang();
                brg.setIdBarang(rs.getInt(1));
                brg.setNamaBarang(rs.getString(2));
                brg.setJenisBarang(rs.getString(3));
                brg.setHargaBarang(rs.getInt(4));
                brg.setDiskripsiBarang(rs.getString(5));
                brg.setIdToko(rs.getInt(6));
                barang.add(brg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return barang;
    }

    //one
    public List<Barang> selectone(int id) throws Exception {
        List<Barang> barang = new ArrayList<Barang>();
        String sql = "SELECT * FROM barang where ID_BARANG = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setInt(1, id);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Barang brg = new Barang();
                brg.setIdBarang(rs.getInt(1));
                brg.setNamaBarang(rs.getString(2));
                brg.setJenisBarang(rs.getString(3));
                brg.setHargaBarang(rs.getInt(4));
                brg.setDiskripsiBarang(rs.getString(5));
                brg.setIdToko(rs.getInt(6));
                barang.add(brg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return barang;
    }

    //select one
    public Barang selectonex(int id) {
        Barang brg = new Barang();
        String sql = "SELECT * FROM barang WHERE ID_BARANG = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setInt(1, id);
            ResultSet rs = st.executeQuery();
            if (rs != null) {
                if (rs.next()) {
                    brg.setIdBarang(rs.getInt(1));
                    brg.setNamaBarang(rs.getString(2));
                    brg.setJenisBarang(rs.getString(3));
                    brg.setHargaBarang(rs.getInt(4));
                    brg.setDiskripsiBarang(rs.getString(5));
                    brg.setIdToko(rs.getInt(6));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return brg;
    }

    public void add(String nama, String jenis, String harga, String dis) throws Exception {
        String sql = "INSERT INTO barang VALUES (NULL, ?, ?, ?, ?, 1)";
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, nama);
            st.setString(2, jenis);
            st.setString(3, harga);
            st.setString(4, dis);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveupload(Barang brg){
        String sql = "INSERT INTO barang VALUES (NULL, ?, ?, ?, ?, ?, 1)";
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, brg.getNamaBarang());
            st.setString(2, brg.getJenisBarang());
            st.setString(3, String.valueOf(brg.getHargaBarang()));
            st.setString(4, brg.getDiskripsiBarang());
            st.setString(5, brg.getFoto());
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void update(int id, String nama, String jenis, String harga, String dis) throws Exception {
        String sql = "UPDATE barang SET NAMA_BARANG=?, JENIS_BARANG=?, HARGA_BARANG=?, DISKRIPSI_BARANG=? WHERE ID_BARANG=?";
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, nama);
            st.setString(2, jenis);
            st.setString(3, harga);
            st.setString(4, dis);
            st.setInt(5, id);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delete(int idBarang) {

        String sql = "DELETE FROM barang WHERE ID_BARANG = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setInt(1, idBarang);

            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
